package com.example.aisqlmcp.mcp.tool;


import com.example.aisqlmcp.mcp.service.APIHttp;
import com.example.aisqlmcp.mcp.util.MCPContextUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SqlTool {
    private static final Logger logger = LoggerFactory.getLogger(SqlTool.class);
    
    @Autowired
    private APIHttp apiHttp;

    @Tool(name="",description = "用户名")
    public String test(String name) {
        String apiKey = MCPContextUtil.getCurrentApiKey();
        logger.info("Received request with API key: {}", apiKey);
        return "hello " + name;
    }
    
    @Tool(name="baidu_http", description = "调用百度网站")
    public String http(String keyword) {
        String apiKey = MCPContextUtil.getCurrentApiKey();
        logger.info("Calling baidu with API key: {}", apiKey);
        
        try {
            if (keyword == null || keyword.isEmpty()) {
                // 如果没有提供关键词，则访问百度首页
                return apiHttp.callBaidu().block();
            } else {
                // 如果提供了关键词，则进行搜索
                return apiHttp.searchBaidu(keyword).block();
            }
        } catch (Exception e) {
            logger.error("Failed to call baidu: ", e);
            return "调用百度失败: " + e.getMessage();
        }
    }
}